package is.glif.fenius.translators.argia.db;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * utility class that sets up Hibernate
 * 
 * @author haniotak
 *
 */
public class HibernateSessionUtil {
    protected static final SessionFactory sessionFactory; 
    public static SessionFactory getSessionfactory() {
        return sessionFactory;
    }

    private static Logger log = Logger.getLogger(HibernateSessionUtil.class);
    static { 
        try { 
            // Create the SessionFactory from hibernate.cfg.xml 
            sessionFactory = new Configuration().configure() .buildSessionFactory(); 
        } catch (Throwable ex) { 
          // Make sure you log the exception, as it might be swallowed 
             System.err.println("Initial SessionFactory creation failed." + ex); 
             log.error(ex);
             throw new ExceptionInInitializerError(ex); 
         } 
    } 
    
    public static void shutdown() {
        System.out.println("shutting down db connection");
        Session session = sessionFactory.openSession(); 
        session.close();
        
    }

}
